// pages/auditList/auditList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showName: '',
    prolist: [],
    // 搜索
    serach_pro: "",
    isUpdate: false,
    pageNum: 1,
    pageSize: 5,
    prosum: '',
    userInfo: '',
    tabList: ["已审核", "待审核"],
    activeTab: '',
    roleType: '',
    tabName: '',
    ischeckAll: false
  },
  goDetail (e) {
    const type = this.data.tabName == '已审核' ? '2' : '1'
    wx.navigateTo({
      url: `../../sub_shenpi/detail/detail?id=${e.currentTarget.dataset.id}&type=${type}`
    })
  },
  ischeckAllChange (e) {
    this.setData({
      ischeckAll: e.detail
    })
    // 全选/取消
    this.data.prolist.forEach(pre => {
      pre.checked = e.detail
    })
    this.setData({
      prolist: this.data.prolist
    })
  },
  goSearch () {
    // console.log('进行搜索')
    this.setData({
      pageNum: 1,
      prolist: [],
      prosum: ''
    })
    this.getList();
  },
  goOnChange (e) {
    this.setData({
      serach_pro: e.detail,
    });
  },
  selectOne (e) {
    // console.log(e, 'e')
    const oneObj = this.data.prolist.find(val => val.id == e.currentTarget.dataset.key)
    if (oneObj.checked) {
      oneObj.checked = false
      this.setData({
        ischeckAll: false
      })
    } else {
      oneObj.checked = true
      const theOne = this.data.prolist.findIndex(pre => !pre.checked)
      if (theOne === -1) {
        this.setData({
          ischeckAll: true
        })
      }
    }
    this.setData({
      prolist: this.data.prolist
    })
  },
  selectTab (e) {
    if (e.detail.title == this.data.tabName) return
    this.setData({
      tabName: e.detail.title,
      pageNum: 1,
      prolist: [],
      prosum: '',
      ischeckAll: false
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    const userInfo = wx.getStorageSync('userInfo')
    // 判断进入的角色
    if (userInfo.corpRole) {
      if (userInfo.corpRole.includes('fir_admin')) {
        this.setData({
          roleType: '1'
        })
      } else {
        this.setData({
          roleType: '2'
        })
      }
    } else {
      this.setData({
        roleType: ''
      })
    }
    // 是否需要默认待审核
    const type = options.type || '1'
    this.setData({
      tabName: type === '1' ? '待审核' : '已审核',
      activeTab: type === '1' ? 1 : 0,
    })
    this.getList()
  },
  isNumber (val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
      return true;
    } else {
      return false;
    }
  },
  getList () {
    let _this = this;
    this.setData({
      loading: true,
    })
    wx.showLoading({
      title: "加载中",
    });
    const params = {
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum,
    }
    if (this.isNumber(this.data.serach_pro)) {
      params.phone = this.data.serach_pro
    } else {
      params.name = this.data.serach_pro
    }
    console.log(params, 'params')
    const url = this.data.tabName == '已审核' ? '/user/query_handled_apply_list' : '/user/apply_wait_list'
    getApp()
      .globalData.request(url, params, "POST")
      .then((res) => {
        console.log(res, 'res')
        setTimeout(function () {
          wx.hideLoading();
        }, 500);
        let rstObj = res;
        if (rstObj.data) {
          // 全选处理
          rstObj.data.list.forEach(poi => {
            poi.checked = this.data.ischeckAll
          })
          _this.setData({
            prolist: this.data.prolist.concat(rstObj.data.list),
            prosum: rstObj.data.total,
          });
          this.setData({
            loading: false,
          })
          console.log(this.data.prolist, "prolist");
        }
      });
  },
  audit (e) {
    const useIndex = this.data.prolist.findIndex(val => val.checked)
    if (useIndex === -1) {
      wx.showToast({
        title: '请至少勾选一条进行审核'
      })
      return
    }
    const idList = this.data.prolist.filter(ped => ped.checked).map(pre => pre.id)
    // console.log(idList, 'idList')
    if (e.currentTarget.dataset.type == '1') {
      // 拒绝
      const params = {
        status: '3',
        idList: idList
      }
      getApp()
        .globalData.request('/user/handle_binding_apply', params, "POST")
        .then((res) => {
          // console.log(res, 'res')
          if (res.code == 200) {
            wx.showToast({
              title: '审核成功',
              icon: 'success'
            })
            this.setData({
              pageNum: 1,
              prolist: [],
              prosum: '',
              ischeckAll: false
            })
            this.getList()
          }
        });
    } else {
      // 通过
      const params = {
        status: '2',
        idList: idList
      }
      getApp()
        .globalData.request('/user/handle_binding_apply', params, "POST")
        .then((res) => {
          // console.log(res, 'res')
          if (res.code == 200) {
            wx.showToast({
              title: '审核成功',
              icon: 'success'
            })
            this.setData({
              pageNum: 1,
              prolist: [],
              prosum: '',
              ischeckAll: false
            })
            this.getList()
          }
        });
    }
  },
  scrollBottom () {
    this.onBottom()
  },
  onBottom () {
    if (this.data.prolist.length >= this.data.prosum) return
    this.data.pageNum++;
    this.getList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    console.log(this.data.isUpdate, 'this.data.isUpdate')
    // if (this.data.name !== this.data.form.form_bank) {
    //   this.setData({
    //     ["form.form_bank"]: this.data.name,
    //     ["form.form_bank1"]: '',
    //   })
    // } else {
    //   this.setData({
    //     ["form.form_bank"]: this.data.name,
    //     ["form.form_bank1"]: this.data.branchName
    //   })
    // }
    if (this.data.isUpdate) {
      this.setData({
        pageNum: 1,
        prolist: [],
        prosum: ''
      })
      this.getList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})